package com.example.springbook.controller;

import com.example.springbook.bean.Book;
import com.example.springbook.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(value = "/book")
public class BookController {
    @Autowired
    private BookService bookService;

    @RequestMapping(value = "/book", method = RequestMethod.POST)
    public boolean addBook(@RequestBody Book book) {
        System.out.println("新增书籍：");
        return bookService.addBook(book);
    }

    @RequestMapping(value = "/book", method = RequestMethod.PUT)
    public boolean updateBook(@RequestBody Book book) {
        System.out.println("更新书籍：");
        return bookService.updateBook(book);
    }

    @RequestMapping(value = "/lendbook", method = RequestMethod.PUT)
    public boolean lendBook(@RequestParam(value = "bookid", required = true) int bookid) {
        System.out.println("借阅书籍：");
        return bookService.lendBook(bookid);
    }

    @RequestMapping(value = "/returnbook", method = RequestMethod.PUT)
    public boolean returnBook(@RequestParam(value = "bookid", required = true) int bookid) {
        System.out.println("还书：");
        return bookService.returnBook(bookid);
    }
    @RequestMapping(value = "/book", method = RequestMethod.DELETE)
    public boolean deleteBook(@RequestParam(value = "bookid", required = true) int bookId) {
        System.out.println("删除书籍：");
        return bookService.deleteBook(bookId);
    }

    @RequestMapping(value = "/surplus", method = RequestMethod.GET)
    public int findSurplus(@RequestParam(value = "bookid", required = true) int bookid) {
        System.out.println("查询书籍余量：");
        return bookService.findSurplus(bookid);
    }

    @RequestMapping(value = "/book", method = RequestMethod.GET)
    public List<Book> findByBookName(@RequestParam(value = "bookName", required = true) String bookName) {
        System.out.println("查询书籍：");
        return bookService.findByBookName(bookName);
    }

    @RequestMapping(value = "/bookAll", method = RequestMethod.GET)
    public List<Book> findByBookAll() {
        System.out.println("查询所有数据：");
        return bookService.findBookAll();
    }
}
